<?php

/**
 * Update Klasse für das Monitoring der SMT Anwedung
 * 
 * @author    Werner Pallentin <werner.pallentin@outlook.de>
 * @package   SMT
 */
class Ticket {
  
  /**
   * Ticket und Antworten löschen
   * @param int $id
   */
  public function deleteTicket($id) {
    $db = new Database('SMT-ADMIN');
    $db->getQuery("DELETE FROM wos_ticket WHERE id=:id || tid=:id", array(':id'=>$id));
  }
  
  /**
   * Methode zum auflisten der eigenen Tickets
   * @return array 
   */
  public function listMyTickets($sUsername) {
    $db = new Database('SMT-ADMIN');  
    $db->getQuery("SELECT *, DATE_FORMAT(datum,'%d.%m.%Y %H:%i') AS niceDatum FROM wos_ticket WHERE zuordnung=:zuordnung AND tid=:tid ORDER BY id DESC", array(':zuordnung'=>$sUsername, ':tid' => 0));
  
    return $db->getValue();
  }
  
  /**
   * Methode zum auflisten aller Tickets
   * @return array 
   */
  public function listTickets() {
    $db = new Database('SMT-ADMIN');
    $ses  = Session::getInstance();
   
    $sum = $db->getQuery("SELECT count(id) FROM wos_ticket WHERE tid=:tid ORDER BY id DESC", array(':tid'=>0), True);
    $ses->set('db_summe', $sum['0']['count(id)']);
    
    $db->getQuery("SELECT *, DATE_FORMAT(datum,'%d.%m.%Y %H:%i') AS niceDatum FROM wos_ticket WHERE tid=:tid ORDER BY id DESC", array(':tid'=>0));
    
    return $db->getValue();
  }

  /**
   * Details zu einem Ticket einlesen
   * @param int $id
   * @return array
   */
  public function readTicket($id) {
    $db = new Database('SMT-ADMIN');
    $ticket = array();
    
    $ticket['ticket'] = $db->getQuery("SELECT *, DATE_FORMAT(datum,'%d.%m.%Y %H:%i') AS niceDatum, DATE_FORMAT(abgabetermin,'%d.%m.%Y') AS niceAbgabetermin FROM wos_ticket WHERE id=:id", array(':id'=>$id), True);
    $ticket['comment'] = $db->getQuery("SELECT *, DATE_FORMAT(datum,'%d.%m.%Y %H:%i') AS niceDatum, DATE_FORMAT(abgabetermin,'%d.%m.%Y') AS niceAbgabetermin FROM wos_ticket WHERE tid=:id ORDER BY id ASC", array(':id'=>$id), True);
    
    return $ticket;
  }
  
  /**
   * Methode zum speichern eines Tickets
   * @param array $post
   * @return int $id
   */
  public function saveNewTicket($post) {
    $db = new Database('SMT-ADMIN');
    $db->getQuery("INSERT INTO wos_ticket (datum) VALUE (:datum)", array(':datum' => date("Y-m-d H:i:s")));
    
    $id = $db->getLastID();
    $this->updateTicket($post, $id);
    
    return $id;
  }
  
  /**
   * Methode zum updaten eines Eintrags
   * @param array $post
   * @param int $id
   */
  public function updateTicket($post, $id) {
    $db = new Database('SMT-ADMIN');
    
    $aData = array();
    
    $aData[':id'] = $id;
    $aData[':user'] = $_SESSION['username'];
    $query = 'user=:user,';

    foreach ($post AS $name => $value) {
      if(is_array($value)) {
        $aData[':' . $name] = implode(',',$value);
        $query .= $name . '=:' . $name . ',';
      } else {
        $aData[':' . $name] = $value;
        $query .= $name . '=:' . $name . ',';
      }
    }

    $db->getQuery("UPDATE wos_ticket SET " . substr($query, 0, -1) . " WHERE id=:id", $aData, False);
    
    if(isset($_POST['tid'])) {
      $db->getQuery("UPDATE wos_ticket SET zuordnung=:zuordnung WHERE id=:tid", array(':zuordnung'=>$_POST['zuordnung'], ':tid'=>$_POST['tid']));
    }
  }
  
}

?>
